package middleware

import (
	"fitness/go-admin/pkg/logger"
	"time"

	"github.com/gin-gonic/gin"
)

func Logger() gin.HandlerFunc {
	return func(c *gin.Context) {
		startTime := time.Now()

		c.Next()

		latency := time.Since(startTime)
		statusCode := c.Writer.Status()
		method := c.Request.Method
		path := c.Request.URL.Path
		clientIP := c.ClientIP()
		requestID, _ := c.Get("requestId")

		logger.Info("HTTP请求",
			"requestId", requestID,
			"method", method,
			"path", path,
			"status", statusCode,
			"latency", latency.String(),
			"ip", clientIP,
		)
	}
}
